"""

查询SQL语句输写入到文件中
"""
from pymysql import Connection;


class Text:
    def __init__(self, path, content):
        f = open(path, 'a', encoding='utf-8')
        f.write(content)
        f.write('\n')
        f.close()


conn = Connection(
    host='localhost',
    port=3306,
    user='root',
    password='123456'
)

# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db('py_sql')
cursor.execute('select cast(order_date as char),order_id,money,province from orders')
results: tuple = cursor.fetchall()
dict_list = []
# print(results)
for item in results:
    dict_list.append({'order_date': item[0], 'order_id': item[1], 'money': item[2], 'province': item[3]})

for i in dict_list:
    # print(dict_list, '你好')
    text = Text('D:/3_ProjectCode/Python/12_sql/SQL写出.txt', str(i))

# 关闭数据库链接
conn.close()
